import Card from '@/base/Card'
import { Suit, Rank } from '@/types'

export const sortDoudizhuCard = (cards: Card[]): Card[] => {
  const rankOrder: Record<Rank, number> = {
    '2': 15,
    A: 14,
    K: 13,
    Q: 12,
    J: 11,
    '10': 10,
    '9': 9,
    '8': 8,
    '7': 7,
    '6': 6,
    '5': 5,
    '4': 4,
    '3': 3,
  }

  const suitOrder: Record<Suit, number> = {
    '♠️': 4, // 黑桃最大
    '♥️': 3,
    '♣️': 2,
    '♦️': 1,
  }

  cards.sort((a, b) => {
    if (a.suit === '🃏') {
      return -1
    }
    if (b.suit === '🃏') {
      return 1
    }

    // 先按牌面大小排序
    const rankDiff = rankOrder[b.rank as Rank] - rankOrder[a.rank as Rank]
    if (rankDiff !== 0) return rankDiff

    // 牌面相同时按花色排序
    return suitOrder[b.suit] - suitOrder[a.suit]
  })
  return cards
}
